package ru.belokuriha_online.resort_hotels_belokuriha;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.android.gms.ads.*; //vrem
import android.webkit.WebView;

public class AboutSanator extends Activity {
	public static File EDIR = Environment.getExternalStorageDirectory();	
	String filepricesPath1;	
	String fileaboutPath;
	String abouts1;
	String abouts2;
	String ssanatori_name2;
	String iid_name2;
	String spprisepath;
	String spprisepathfull;
	String abousant;
	ImageView aboutI;
	WebView webViewsan;
	Drawable dia;
	int FlagOnlain;
	HttpURLConnection urlConnectiond;
	XmlPullParser parser;
	InputStream xmlfile;
	
	private CharSequence[] dopmenu;
	AlertDialog.Builder dialogdopmenu;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.aboutsanator);
		try {
		dopmenu = getResources().getTextArray(R.array.dopmenu);
		ssanatori_name2 = getIntent().getExtras().getString("san_name1");
		iid_name2 = getIntent().getExtras().getString("id_name1");
		spprisepath = getIntent().getExtras().getString("spprisepath");
		spprisepathfull = getIntent().getExtras().getString("spprisepathfull");
		FlagOnlain = getIntent().getExtras().getInt("FlagOnlain");
		
		LinearLayout panel2 = (LinearLayout) findViewById(R.id.panel3);
		LayoutInflater panellInflater = getLayoutInflater();
		View panellorg1 = panellInflater.inflate(R.layout.panel, panel2, true);

		LinearLayout reklama = (LinearLayout) findViewById(R.id.reklamagoo3);
		LayoutInflater reklamaInflater = getLayoutInflater();
		try {
			View reklamaorg = reklamaInflater.inflate(R.layout.reklamagoo, reklama, true);
			AdView adView = (AdView)reklamaorg.findViewById(R.id.adView); //vrem
			AdRequest adRequest = new AdRequest.Builder().build();
			adView.loadAd(adRequest); //vrem
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		try {
			
			XmlPullParserFactory parserpull = XmlPullParserFactory.newInstance();
			parserpull.setValidating(false);
			parser = parserpull.newPullParser();
			File xmlfileprices1 = new File(spprisepathfull);
			xmlfile = new FileInputStream(xmlfileprices1);
			parser.setInput(xmlfile, null);
			
			
			while (parser.getEventType()!= XmlPullParser.END_DOCUMENT) {
				if  (parser.getEventType() == XmlPullParser.START_TAG
						&& parser.getName().equals(iid_name2 + "_short") 
						&& parser.getDepth() == 3) {

					abouts1 = parser.getAttributeValue(0);			
					abouts2 = parser.nextText();

				}
				parser.next();
			}
			xmlfile.close();
		}
		catch (Throwable t) {

		}

		///
		
		TextView anameView = (TextView)findViewById(R.id.astextv);		
		anameView.setText(ssanatori_name2);
		
		abousant = abouts2;
		if (abouts2== null) {
			abousant =" ";	
		}
			try {
				
				webViewsan = (WebView)findViewById(R.id.webViewsan);		
				webViewsan.getSettings();
				webViewsan.getSettings().setJavaScriptEnabled(false);
				webViewsan.setBackgroundColor(Color.parseColor("#629CE6"));
				webViewsan.loadDataWithBaseURL(null,Html.fromHtml("<![CDATA["+ abousant+"]]>").toString(), "text/html", "UTF-8", null);
		
			} catch (Exception e) {
				// TODO: handle exception
			}
		if (abouts1 == null) {
			abouts1 = ""; 
		}
		try {
			if (!abouts1.equals("")) {
				if (FlagOnlain == 0) {
				fileaboutPath = spprisepath+File.separator + abouts1;
				} else
				{
					fileaboutPath = spprisepath + abouts1;	
				}
				/*File aboutpic = new File(fileaboutPath);
				InputStream ia = new FileInputStream(aboutpic);

				dia = Drawable.createFromStream(ia, null);
				aboutI = (ImageView)findViewById(R.id.aboutimage);
				aboutI.setVisibility(View.VISIBLE);
				aboutI.setImageDrawable(dia);
				
				ia.close();
				*/
				aboutI = (ImageView)findViewById(R.id.aboutimage);
				aboutI.setVisibility(View.VISIBLE);
				aboutI.setImageURI(Uri.parse(fileaboutPath));

			}

		}
		catch(Exception ex) {
			
		}

		///
		try {
		aboutI.setOnClickListener(new View.OnClickListener() {				
			public void onClick(View v) {
				try {
					Intent intentpicsan = new Intent(getApplicationContext(), Picsanator.class);
					intentpicsan.putExtra("spprisepath", spprisepath);
					intentpicsan.putExtra("fileaboutPath", fileaboutPath);
					intentpicsan.putExtra("FlagOnlain", FlagOnlain);
					startActivityForResult(intentpicsan,0);
					} catch (Exception e) {

					}

			}	
		});
		} catch (Exception e) {
			// TODO: handle exception
		}
		final ImageButton zv1 =(ImageButton) panellorg1.findViewById(R.id.imageButton1);
		zv1.setOnClickListener(new View.OnClickListener() {
			TransitionDrawable TelfTransition = (TransitionDrawable)getApplicationContext().getResources().getDrawable(R.drawable.transition_telef);	
			public void onClick(View v) {
				try {
					zv1.setImageDrawable(TelfTransition);
					TelfTransition.startTransition(1000);
					String stel = getString(R.string.telefonmob);
					Intent diallintent = new Intent(Intent.ACTION_DIAL,Uri.parse(stel));
					startActivity(diallintent);
				} catch (Exception e) {

				}

			}	
		});

		final ImageButton eml =(ImageButton) panellorg1.findViewById(R.id.imageButton2);
		eml.setOnClickListener(new View.OnClickListener() {
			TransitionDrawable EmlTransition = (TransitionDrawable)getApplicationContext().getResources().getDrawable(R.drawable.transition_eml);
			public void onClick(View v) {
				try {
					eml.setImageDrawable(EmlTransition);
					EmlTransition.startTransition(1000);
					String smail = getString(R.string.email);
					Intent emalintent = new Intent(Intent.ACTION_VIEW,Uri.parse(smail));
					startActivity(emalintent);
				} catch (Exception e) {

				}	

			}	
		});
		final ImageButton abt =(ImageButton) panellorg1.findViewById(R.id.imageButton4);
		abt.setOnClickListener(new View.OnClickListener() {
			TransitionDrawable AboutTransition = (TransitionDrawable)getApplicationContext().getResources().getDrawable(R.drawable.transition_about);
			public void onClick(View v) {
				try {
					abt.setImageDrawable(AboutTransition);
					AboutTransition.startTransition(1000);
					Intent intentab = new Intent(getApplicationContext(), About.class);
					intentab.putExtra("spprisepath", spprisepath);
					intentab.putExtra("FlagOnlain", FlagOnlain);
					startActivityForResult(intentab,0);
				} catch (Exception e) {

				}
			}	
		});
		final ImageButton brouz =(ImageButton) panellorg1.findViewById(R.id.imageButton5);
		brouz.setOnClickListener(new View.OnClickListener() {
			TransitionDrawable BrTransition = (TransitionDrawable)getApplicationContext().getResources().getDrawable(R.drawable.transition_browser);
			public void onClick(View v) {
				try {
					brouz.setImageDrawable(BrTransition);
					BrTransition.startTransition(1000);
					String sweb = getString(R.string.web);
					Intent brouzintent = new Intent(Intent.ACTION_VIEW,Uri.parse(sweb));
					startActivity(brouzintent);
				} catch (Exception e) {

				}
			}	
		});


		final ImageButton homes =(ImageButton) panellorg1.findViewById(R.id.imageButton3);
		homes.setOnClickListener(new View.OnClickListener() {
			TransitionDrawable HomTransition = (TransitionDrawable)getApplicationContext().getResources().getDrawable(R.drawable.transition_hom);
			public void onClick(View v) {
				try {
					homes.setImageDrawable(HomTransition);
					HomTransition.startTransition(1000);			
					setResult(3);
					AboutSanator.super.onDestroy();
					finish();
					AboutSanator.super.onDestroy();
				} catch (Exception e) {

				}
			}	
		});
		homes.setOnLongClickListener(new View.OnLongClickListener() {
			TransitionDrawable HomTransition = (TransitionDrawable)getApplicationContext().getResources().getDrawable(R.drawable.transition_hom);	
			@Override
			public boolean onLongClick(View v) {
				homes.setImageDrawable(HomTransition);
				HomTransition.startTransition(1000);
					Dopmenu();
				return false;
			}
		});
		final ImageButton srav =(ImageButton) panellorg1.findViewById(R.id.imageButtonsrav);
		srav.setOnClickListener(new View.OnClickListener() {
			TransitionDrawable sravTransition = (TransitionDrawable)getApplicationContext().getResources().getDrawable(R.drawable.transition_compare);
			public void onClick(View v) {
				try {
					srav.setImageDrawable(sravTransition);
					sravTransition.startTransition(1000);
					Intent intentsea = new Intent(getApplicationContext(), Sravneniye.class);
					intentsea.putExtra("san_name", ssanatori_name2);
					intentsea.putExtra("id_name", iid_name2);
					intentsea.putExtra("spprisepath", spprisepath);
					intentsea.putExtra("spprisepathfull", spprisepathfull);
					intentsea.putExtra("FlagOnlain", FlagOnlain);
					Integer flags_srav = 3;
					intentsea.putExtra("flags_srav", flags_srav);
					startActivityForResult(intentsea,3);
					
				} catch (Exception e) {

				}
			}	
		});
	} catch (Exception e) {
		// TODO: handle exception
	}	
	}

	public boolean onCreateOptionsMenu(Menu menu) { 

		getMenuInflater().inflate(R.menu.pmenu, menu);

		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) { 

		if (FlagOnlain == 0) {
			menu.findItem(R.id.loaditem1).setVisible(true);
			menu.findItem(R.id.loaditem1).setEnabled(true);
			} else
			{			
			menu.findItem(R.id.loaditem1).setEnabled(false);
			menu.findItem(R.id.loaditem1).setVisible(false);
			}
			menu.findItem(R.id.pitem2).setEnabled(true);
			menu.findItem(R.id.abitem3).setEnabled(true);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) { 

		case R.id.loaditem1: 
			GoMain();
			break; 


		case R.id.pitem2:

			try {
				Intent Preferintent = new Intent(getApplicationContext(),PreferActivity.class);
				Preferintent.putExtra("FlagOnlain", FlagOnlain);
				startActivityForResult(Preferintent, 0);
			} catch (Exception e) {

			}
			break; 
		case R.id.abitem3: 

			try {
				Intent intentapp = new Intent(getApplicationContext(), Aboutapp.class);
				intentapp.putExtra("FlagOnlain", FlagOnlain);
				startActivityForResult(intentapp, 0);
			} catch (Exception e) {

			}
			break;
		}        


		return super.onOptionsItemSelected(item); 

	}
	private void GoMain() {
		AlertDialog.Builder buildergomain = new AlertDialog.Builder(this);
		buildergomain.setTitle(R.string.lt_app);		
		buildergomain.setMessage(R.string.lmmm_app);		
		buildergomain.setNegativeButton(R.string.qn_app, null);
		buildergomain.setPositiveButton(R.string.qy_app, new OnClickListener() {
			public void onClick(DialogInterface arg0, int arg1) {
				setResult(7);
				AboutSanator.super.onDestroy();
				finish();
				AboutSanator.super.onDestroy();

			}
		}).create().show();	
	}
	public void Dopmenu() {
		dialogdopmenu = new AlertDialog.Builder(this);
		dialogdopmenu.setTitle("");		
		dialogdopmenu.setItems(dopmenu, 
				new DialogInterface.OnClickListener() {					
			@Override
			public void onClick(DialogInterface dialog, int which) {
				try { 
					openOptionsMenu();				
				} catch (Exception e) {
	
				}						
		}
	}				
			).create().show();


}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == 7) {
			setResult(7);
			AboutSanator.super.onDestroy();
			finish();
			AboutSanator.super.onDestroy();
		}

		if (resultCode ==  5) {
			try {
				openOptionsMenu();
			} catch (Exception e) {

			}
		}
		if (resultCode ==  9) {
			try {
				Intent Preferintent = new Intent(getApplicationContext(),PreferActivity.class);
				Preferintent.putExtra("FlagOnlain", FlagOnlain);
				startActivityForResult(Preferintent,0);
			} catch (Exception e) {

			}
		}
		if (resultCode ==  10) {
			try{
				Intent intentapp = new Intent(getApplicationContext(), Aboutapp.class);
				intentapp.putExtra("FlagOnlain", FlagOnlain);
				startActivityForResult(intentapp,0);	
			} catch (Exception e) {

			}
		}

	}
	@Override 
	protected void onDestroy() {
		super.onDestroy();
		webViewsan.clearCache(true);
		webViewsan.clearHistory();
		webViewsan.removeAllViews();
		webViewsan.destroy();
		try {
			aboutI.destroyDrawingCache();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		System.gc();
		
	}
	@Override
	public void onBackPressed() {
		System.gc();
		AboutSanator.super.onDestroy();
		finish();
		AboutSanator.super.onDestroy();
	}

}
